package com.zhangln.mountbuzhou.dao.impl;

import com.alibaba.fastjson.JSONObject;
import com.zhangln.mountbuzhou.dto.request.PushMsgReq;
import com.zhangln.mountbuzhou.entity.TbPushLogEntity;
import com.zhangln.mountbuzhou.mapper.TbPushLogMapper;
import com.zhangln.mountbuzhou.dao.TbPushLogDao;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 推送日志 服务实现类
 * </p>
 *
 * @author sherry
 * @since 2020-04-27
 */
@Service
@Slf4j
public class TbPushLogDaoImpl extends ServiceImpl<TbPushLogMapper, TbPushLogEntity> implements TbPushLogDao {

    @Override
    public void savePushLog(String pushId, String channelId, PushMsgReq pushMsgReq) {
        TbPushLogEntity tbPushLogEntity = TbPushLogEntity.builder()
                .pushId(pushId)
                .msgId(pushMsgReq.getMsgId())
                .channelId(channelId)
                .msg(JSONObject.toJSONString(pushMsgReq))
                .callBackUrl(pushMsgReq.getUrl())
                .times(1)
                .status(1)
                .note("推送成功")
                .revision(0)
                .createdBy("-1")
                .createdTime(LocalDateTime.now())
                .updatedBy("-1")
                .updatedTime(LocalDateTime.now())
                .build();
        log.info("新增推送日志：\n\t\t{}", tbPushLogEntity);
        this.save(tbPushLogEntity);
    }

    @Override
    public void truncate() {
        this.baseMapper.truncate();
    }

    @Override
    public void msgConfirm(String channelId, String msgId) {
        this.baseMapper.msgConfirm(channelId, msgId);
    }

}
